﻿@model DatabaseEntities
@{
    ViewBag.Title = "DTweet | A dashing way of tweeting";

    var tweets = Model.Tweets.Where(t => t.SharedTo == "Public");
    var currentUserToken = "NaN";
    var anyUser = Static.CurrentUser != null;
    if (anyUser)
    {
        currentUserToken = Static.CurrentUser.UniqueToken;
        tweets = tweets.Where(t => t.SharedTo == "Public" || t.SharedTo.Contains(currentUserToken));
    }
    tweets = tweets.OrderByDescending(t => t.Id).Take(20);
    
    var user = Static.CurrentUser;
}
@section UserName
{    
    @if (user != null)
    {
        <div id="you" accesskey="@user.UniqueToken">@user.FullName.Truncate(20)</div>
    }
}

<div id="tweets-container">
    @foreach (var tweet in tweets)
    {
        var tweetBy = tweet.UserToken.GetAccountByUserToken(Model);
        <div>
            @* << This DIV is needed at DELETE action; for removing whole post: including user-image *@
            <div class="user-image">
                <img src="@tweetBy.Gravatar" itemprop="image" width="60" height="60" alt="" /></div>
            <div class="tweet-section" id="@tweet.TweetToken">
                <div class="manage-tweet">
                </div>
                @if (tweet.UserToken == currentUserToken)
                { <input id="owner" type="hidden" value="owner" /> }
                else
                { <input id="owner" type="hidden" value="not-owner" /> }
                <div id="post-options" class="post-options">
                </div>
                <h2 class="name-section">
                    <a href="@Url.Content("~/" + tweet.UserToken)">@tweetBy.FullName</a>
                    -
                    @if (tweet.EditingTime != null)
                    {
                        <a href="@Url.Content("~/" + tweet.TweetToken + "/" + tweet.Message.FetchPostUrl())" class="p-Link">
                            <span class="time"><time class="time" itemprop="date" datetime="@tweet.TweetingTime.ToMachineTime()" pubdate>@tweet.TweetingTime</time></span>
                            <span class="edit-time">
                                <abbr title="Last edit time: @tweet.EditingTime">
                                    (@tweet.EditCount.GetEditTimes())</abbr>
                            </span></a>
                    }
                    else
                    {
                        <a href="@Url.Content("~/" + tweet.TweetToken + "/" + tweet.Message.FetchPostUrl())" class="p-Link">
                            <span class="time"><time class="time" itemprop="date" datetime="@tweet.TweetingTime.ToMachineTime()" pubdate>@tweet.TweetingTime</time></span>
                        </a>
                    }
                </h2>
                <div class="tweet-message">
                    @Html.Raw(tweet.Message.HandleAllThings())
                </div>
            </div>
        </div>
    }
</div>
@section JavaScript
{
    <script src="@Url.Content("~/Content/JavaScript/JsDecoder.js")" type="text/javascript"> </script>
    <script src="@Url.Content("~/Content/JavaScript/dashingquill.user.js")" type="text/javascript"> </script>
    <script>
        user.handleEvents();
</script>
}

@if(tweets.Count() <= 0)
{
    <div style="padding: 20px; border: 1px solid red; font-size: 20px; color: red;">
        There is no public tweet yet! <br /> <br />
        <a href="@Url.Content("~/Login")" style="color:blue;">Create New Account</a> (It is free and easy!!)
    </div>
}